package cn.yunhe.springmvc.web;

import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.yunhe.springmvc.model.User;

/**
 * 会话控制器(用HttpSession SERVLET-API实现)
 * 
 * @author YUNEH
 *
 */
@Controller
@RequestMapping("session")
public class SessionController {
	
	/**
	 * 
	 * 用户登陆
	 * 
	 * @param user
	 * @param session
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(User user,HttpSession session){
		if(user!=null){
			session.setAttribute("user", user);
			//返回会员中心
			return "home";
		}else{
			//返回登陆页面
			return "redirect:/login.html";
		}
	}
	
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String logout(HttpSession session){
		System.out.println("会话用户信息为："+session.getAttribute("user"));
		//销毁用户会话
		session.invalidate();
		//返回登陆页面
		return "redirect:/login.html";
	}
	
}
